using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Commerce.Common
{
    /// <summary>
    /// Controller class for CSK_Store_Product
    /// </summary>
    [System.ComponentModel.DataObject]
    public partial class ProductController
    {
        // Preload our schema..
        Product thisSchemaLoad = new Product();
        private string userName = string.Empty;
        protected string UserName
        {
            get
            {
				if (userName.Length == 0) 
				{
    				if (System.Web.HttpContext.Current != null)
    				{
						userName=System.Web.HttpContext.Current.User.Identity.Name;
					}

					else
					{
						userName=System.Threading.Thread.CurrentPrincipal.Identity.Name;
					}

				}

				return userName;
            }

        }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public ProductCollection FetchAll()
        {
            ProductCollection coll = new ProductCollection();
            Query qry = new Query(Product.Schema);
            coll.LoadAndCloseReader(qry.ExecuteReader());
            return coll;
        }

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public ProductCollection FetchByID(object ProductID)
        {
            ProductCollection coll = new ProductCollection().Where("productID", ProductID).Load();
            return coll;
        }

		
		[DataObjectMethod(DataObjectMethodType.Select, false)]
        public ProductCollection FetchByQuery(Query qry)
        {
            ProductCollection coll = new ProductCollection();
            coll.LoadAndCloseReader(qry.ExecuteReader()); 
            return coll;
        }

        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public bool Delete(object ProductID)
        {
            return (Product.Delete(ProductID) == 1);
        }

        [DataObjectMethod(DataObjectMethodType.Delete, false)]
        public bool Destroy(object ProductID)
        {
            return (Product.Destroy(ProductID) == 1);
        }

        
        
    	
	    /// <summary>
	    /// Inserts a record, can be used with the Object Data Source
	    /// </summary>
        [DataObjectMethod(DataObjectMethodType.Insert, true)]
	    public void Insert(int ProductID,string Sku,Guid ProductGUID,string ProductName,string ShortDescription,int ManufacturerID,string AttributeXML,int StatusID,int ProductTypeID,int ShippingTypeID,int ShipEstimateID,int TaxTypeID,string StockLocation,decimal OurPrice,decimal RetailPrice,decimal Weight,string CurrencyCode,string UnitOfMeasure,string AdminComments,decimal Length,decimal Height,decimal Width,string DimensionUnit,bool IsDeleted,int ListOrder,int RatingSum,int TotalRatingVotes,string DefaultImage,DateTime CreatedOn,string CreatedBy,DateTime ModifiedOn,string ModifiedBy,int? Inventory,string UrlFriendly)
	    {
		    Product item = new Product();
		    
            item.ProductID = ProductID;
            
            item.Sku = Sku;
            
            item.ProductGUID = ProductGUID;
            
            item.ProductName = ProductName;
            
            item.ShortDescription = ShortDescription;
            
            item.ManufacturerID = ManufacturerID;
            
            item.AttributeXML = AttributeXML;
            
            item.StatusID = StatusID;
            
            item.ProductTypeID = ProductTypeID;
            
            item.ShippingTypeID = ShippingTypeID;
            
            item.ShipEstimateID = ShipEstimateID;
            
            item.TaxTypeID = TaxTypeID;
            
            item.StockLocation = StockLocation;
            
            item.OurPrice = OurPrice;
            
            item.RetailPrice = RetailPrice;
            
            item.Weight = Weight;
            
            item.CurrencyCode = CurrencyCode;
            
            item.UnitOfMeasure = UnitOfMeasure;
            
            item.AdminComments = AdminComments;
            
            item.Length = Length;
            
            item.Height = Height;
            
            item.Width = Width;
            
            item.DimensionUnit = DimensionUnit;
            
            item.IsDeleted = IsDeleted;
            
            item.ListOrder = ListOrder;
            
            item.RatingSum = RatingSum;
            
            item.TotalRatingVotes = TotalRatingVotes;
            
            item.DefaultImage = DefaultImage;
            
            item.CreatedOn = CreatedOn;
            
            item.CreatedBy = CreatedBy;
            
            item.ModifiedOn = ModifiedOn;
            
            item.ModifiedBy = ModifiedBy;
            
            item.Inventory = Inventory;
            
            item.UrlFriendly = UrlFriendly;
            
	    
		    item.Save(UserName);
	    }

    	
	    /// <summary>
	    /// Updates a record, can be used with the Object Data Source
	    /// </summary>
        [DataObjectMethod(DataObjectMethodType.Update, true)]
	    public void Update(int ProductID,string Sku,Guid ProductGUID,string ProductName,string ShortDescription,int ManufacturerID,string AttributeXML,int StatusID,int ProductTypeID,int ShippingTypeID,int ShipEstimateID,int TaxTypeID,string StockLocation,decimal OurPrice,decimal RetailPrice,decimal Weight,string CurrencyCode,string UnitOfMeasure,string AdminComments,decimal Length,decimal Height,decimal Width,string DimensionUnit,bool IsDeleted,int ListOrder,int RatingSum,int TotalRatingVotes,string DefaultImage,DateTime CreatedOn,string CreatedBy,DateTime ModifiedOn,string ModifiedBy,int? Inventory,string UrlFriendly)
	    {
		    Product item = new Product();
	        item.MarkOld();
	        item.IsLoaded = true;
		    
				item.ProductID = ProductID;
				
				item.Sku = Sku;
				
				item.ProductGUID = ProductGUID;
				
				item.ProductName = ProductName;
				
				item.ShortDescription = ShortDescription;
				
				item.ManufacturerID = ManufacturerID;
				
				item.AttributeXML = AttributeXML;
				
				item.StatusID = StatusID;
				
				item.ProductTypeID = ProductTypeID;
				
				item.ShippingTypeID = ShippingTypeID;
				
				item.ShipEstimateID = ShipEstimateID;
				
				item.TaxTypeID = TaxTypeID;
				
				item.StockLocation = StockLocation;
				
				item.OurPrice = OurPrice;
				
				item.RetailPrice = RetailPrice;
				
				item.Weight = Weight;
				
				item.CurrencyCode = CurrencyCode;
				
				item.UnitOfMeasure = UnitOfMeasure;
				
				item.AdminComments = AdminComments;
				
				item.Length = Length;
				
				item.Height = Height;
				
				item.Width = Width;
				
				item.DimensionUnit = DimensionUnit;
				
				item.IsDeleted = IsDeleted;
				
				item.ListOrder = ListOrder;
				
				item.RatingSum = RatingSum;
				
				item.TotalRatingVotes = TotalRatingVotes;
				
				item.DefaultImage = DefaultImage;
				
				item.CreatedOn = CreatedOn;
				
				item.CreatedBy = CreatedBy;
				
				item.ModifiedOn = ModifiedOn;
				
				item.ModifiedBy = ModifiedBy;
				
			item.Inventory = Inventory;
				
			item.UrlFriendly = UrlFriendly;
				
	        item.Save(UserName);
	    }

    }

}

